package main.com.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class BaseDao {

	//用户名
		private static String user = "";
		//密码
		private static String password = "";
		//驱动
		private static String driver = "";
		//url
		private static String url = "";
		//数据库对象
		private  Connection conn = null;
		//
		private Statement statement = null;
		//
		private ResultSet resultSet = null;
		//
		public BaseDao(){
			ResourceBundle rBundle = ResourceBundle.getBundle("login");
			try {
				user = rBundle.getString("user");
				password = rBundle.getString("password");
				driver = rBundle.getString("driver");
				url = rBundle.getString("url");
				Class.forName(driver);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
		/**
	     * 建立数据库连接
	     * 
	     * @return
	     */ 
	    public Connection getConnection() { 
	        try { 
	            // 获取连接 
	            conn = DriverManager.getConnection(url, user, password);
	        } catch (SQLException e) { 
	            System.out.println(e.getMessage()); 
	        } 
	        return conn; 
	    } 
		//关闭
		public void close() throws Exception{
			if(resultSet!=null){
				resultSet.close();
			}
			if(statement!=null){
				statement.close();
			}
			if(conn!=null){
				conn.close();
			}
		}
		//
		public List queryList(String sql){
			List list = new ArrayList();
			try {
				//
				conn = this.getConnection();
				//
				statement = conn.createStatement();
				//
				resultSet = statement.executeQuery(sql);
				while(resultSet.next()){
					list.add(resultSet.getString(1));
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
			return list;
		}
}
